#include<arduino.h>

/*
 * 1为前进
 * 0为后退
 * 2为左转
 * 3为右转
 */
char serialData;
#define gpio1 A2
#define gpio2 A3  //右后轮，A2正A3负为前进
#define gpio3 A4
#define gpio4 A5  //左后轮，A4正A5负为前进
#define gpio5 7
#define gpio6 4   //右前轮，4正7负为前进
#define gpio7 3
#define gpio8 2   //左前轮，2正3负为前进
#define en1 5     //右前轮
#define en2 6     //左前轮
#define en3 9     //右后轮
#define en4 10    //左后轮
#define v1  500   //前进后退速度
#define v2 500    //左转右转速度

void qianjin(){
      Serial.print("Got command: ");  
      Serial.println(serialData); 
      Serial.println("前进");
      
      digitalWrite(gpio1, HIGH);  
      digitalWrite(gpio2, LOW);
      
      digitalWrite(gpio3, HIGH);  
      digitalWrite(gpio4, LOW);
      
      digitalWrite(gpio5, LOW);  
      digitalWrite(gpio6, HIGH);
      
      digitalWrite(gpio7, LOW);  
      digitalWrite(gpio8, HIGH);
      analogWrite(en1,v1); 
      analogWrite(en2,v1); 
      analogWrite(en3,v1); 
      analogWrite(en4,v1); 
}
void houtui(){
      Serial.print("Got command: ");  
      Serial.println(serialData); 
      Serial.println("后退");
      
      digitalWrite(gpio1, LOW);  
      digitalWrite(gpio2, HIGH);
      digitalWrite(gpio3, LOW);  
      digitalWrite(gpio4, HIGH);
      digitalWrite(gpio5, HIGH);  
      digitalWrite(gpio6, LOW);
      digitalWrite(gpio7, HIGH);  
      digitalWrite(gpio8, LOW);
      analogWrite(en1,v1); 
      analogWrite(en2,v1); 
      analogWrite(en3,v1); 
      analogWrite(en4,v1); 
}
void zuozhuan(){
      Serial.print("Got command: ");  
      Serial.println(serialData); 
      Serial.println("左转");
      
      digitalWrite(gpio1, HIGH);  
      digitalWrite(gpio2, LOW);
      digitalWrite(gpio3, LOW);  
      digitalWrite(gpio4, HIGH);
      digitalWrite(gpio5, LOW);  
      digitalWrite(gpio6, HIGH);
      digitalWrite(gpio7, HIGH);  
      digitalWrite(gpio8, LOW);
      analogWrite(en1,v2); 
      analogWrite(en2,v2); 
      analogWrite(en3,v2); 
      analogWrite(en4,v2); 
}
void youzhuan(){
      Serial.print("Got command: ");  
      Serial.println(serialData); 
      Serial.println("右转");
      
      digitalWrite(gpio1, LOW);  
      digitalWrite(gpio2, HIGH);
      digitalWrite(gpio3, HIGH);  
      digitalWrite(gpio4, LOW);
      digitalWrite(gpio5, HIGH);  
      digitalWrite(gpio6, LOW);
      digitalWrite(gpio7, LOW);  
      digitalWrite(gpio8, HIGH);
      analogWrite(en1,v2); 
      analogWrite(en2,v2); 
      analogWrite(en3,v2); 
      analogWrite(en4,v2);
}
void tingzhi(){
      digitalWrite(gpio1, LOW);  
      digitalWrite(gpio2, LOW);
      digitalWrite(gpio3, LOW);  
      digitalWrite(gpio4, LOW);
      digitalWrite(gpio5, LOW);  
      digitalWrite(gpio6, LOW);
      digitalWrite(gpio7, LOW);  
      digitalWrite(gpio8, LOW);
      analogWrite(en1,0); 
      analogWrite(en2,0); 
      analogWrite(en3,0); 
      analogWrite(en4,0); 
}
void csh(){
      pinMode(gpio1, OUTPUT); 
      pinMode(gpio2, OUTPUT);
      pinMode(gpio3, OUTPUT);
      pinMode(gpio4, OUTPUT); 
      pinMode(gpio5, OUTPUT);
      pinMode(gpio6, OUTPUT); 
      pinMode(gpio7, OUTPUT); 
      pinMode(gpio8, OUTPUT); 
      
      digitalWrite(gpio1, LOW);
      digitalWrite(gpio2, LOW);
      digitalWrite(gpio3, LOW);
      digitalWrite(gpio4, LOW);
      digitalWrite(gpio5, LOW);
      digitalWrite(gpio6, LOW);
      digitalWrite(gpio7, LOW);
      digitalWrite(gpio8, LOW);
}
void setup() {
      Serial.begin(9600);
      csh();
}
 
void loop(){
    serialData =  Serial.read();   
    switch(serialData){
      case '1': qianjin(); break;
      case '0': houtui(); break;
      case '2': zuozhuan(); break;
      case '3': youzhuan(); break;
      case '4': tingzhi(); break;
    }
}
